package com.jxtti.controller;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.jxtti.entity.SolicitPublicComment;
import com.jxtti.service.SolicitPublicCommentService;
import com.jxtti.utils.ExceptionUtil;
/**
 * 民意征集--评论
 * bff 
 */ 
@Controller
@RequestMapping("/solicitPublicCommentController") 
public class SolicitPublicCommentController {
	
	private final static Logger log= Logger.getLogger(SolicitPublicCommentController.class);
	
	@InitBinder  
    public void initBinder(WebDataBinder binder) {     
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");     
        dateFormat.setLenient(false);     
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }
	
	@Autowired
	private SolicitPublicCommentService  solicitPublicCommentService; 
			
	
	@RequestMapping(value="/list", method = RequestMethod.POST) 
	@ResponseBody
	public Map<String, Object>  SolicitPubliclist(@RequestParam Map<String, Object> data, SolicitPublicComment solicitPublicComment, HttpServletRequest request,
			HttpServletResponse response) throws Exception{		
		Map<String, Object> result = new HashMap<String, Object>();	
		
		data.put("is_deleted","0");
		
		PageInfo<SolicitPublicComment> page = solicitPublicCommentService.list(data);
		result.put("total", page.getTotal());
		result.put("rows", page.getList());
		return result;	
	}
	
	
	
	/**
	 * 添加或修改数据
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "/save")
	@ResponseBody
	public Map<String,Object> save(@RequestParam Map<String, Object> data,
			SolicitPublicComment  solicitPublicComment, HttpServletRequest request,
			HttpServletResponse response) throws Exception{

		ModelAndView mav = new ModelAndView();
		try {
			// 保存数据
			//User user = AuthUtil.getUserInfo(request);
			//disStandard.setOperPerson(user.getUserName());
			int exeCount = solicitPublicCommentService.add(solicitPublicComment);
			if (exeCount > 0) {
				data.put("success", true);
			} else {
				data.put("success", false);
			}
		} catch (Exception e) {
			data.put("success", false);
			data.put("error", ExceptionUtil.createStackTrackMessage(e));
			e.printStackTrace();
		}
		return data;
	}
	
	
	
	
	/**
	 * 点击修改跳转到增加界面
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/updateInterface")
	@ResponseBody
	public ModelAndView updateInterface(@RequestParam Map<String, Object> data,
			HttpServletRequest request, SolicitPublicComment  solicitPublicComment,
			HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		try {			
			solicitPublicComment = solicitPublicCommentService.queryById(solicitPublicComment);
			data.put("solicitPublicComment", solicitPublicComment);
			mav.addObject("data", data);			
			mav.setViewName("/pages/Admin/Site/SolicitPublic/SolicitCommentEdit.jsp");
						
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
		
	/**
	 * 修改信息
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> update(@RequestParam Map<String, Object> data,
			SolicitPublicComment  solicitPublicComment, HttpServletRequest request,
			HttpServletResponse response) {
		try {			
			SolicitPublicComment solicitComment=solicitPublicCommentService.queryById(solicitPublicComment);
			solicitComment.setIs_publish(solicitPublicComment.getIs_publish());	//只改变是否 公开
			
			int exeCount = solicitPublicCommentService.update(solicitComment);
			if (exeCount > 0) {
				data.put("success", true);
			} else {
				data.put("success", false);
			}
		} catch (Exception e) {
			data.put("success", false);
			data.put("error", ExceptionUtil.createStackTrackMessage(e));
			e.printStackTrace();
		}
		return data;
	}
		
/**
	 * 删除信息
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Map<String, Object> delete(@RequestParam Map<String, Object> data,
			HttpServletRequest request,
			HttpServletResponse response) {
		try {
			String[] idArr=data.get("ids").toString().split(",");
			for(String id:idArr){
				SolicitPublicComment  solicitPublicComment=new SolicitPublicComment();
				solicitPublicComment.setId(Integer.parseInt(id));	
				solicitPublicComment=solicitPublicCommentService.queryById(solicitPublicComment);
				solicitPublicComment.setIs_deleted(1);
				int exeCount = solicitPublicCommentService.update(solicitPublicComment);
				if (exeCount > 0) {
					data.put("success", true);
				} else {
					data.put("success", false);
				}				
			}			
		} catch (Exception e) {
			data.put("success", false);
			data.put("error", ExceptionUtil.createStackTrackMessage(e));
			e.printStackTrace();
		}
		return data;
	}

}
